import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/pages/Home/homecomp/homeList.dart';
import 'package:flutter_house_manager/pages/Home/homecomp/homeNav.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';
import 'package:flutter_house_manager/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _announceList = [];
  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  getAnnounceList() async {
    try {
      final res = await getAnnounceListAPI();
      // 显示成功提示
      PromptAction.showSuccess('获取公告列表成功');
      setState(() {
        _announceList = res;
      });
    } catch (e) {
      PromptAction.showError('获取公告列表失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('享+社区'),
        centerTitle: true,
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
        children: [
          testToken(),
          const Homenav(),
          Image.asset(
            'assets/images/banner@2x.jpg',
            width: double.infinity,
          ),
          HomeList(list: _announceList),
        ],
      ),
    );
  }
}

testToken() {
  return Row(
    children: [
      ElevatedButton(
          onPressed: () {
            tokenManager.setToken('123456');
          },
          child: const Text('设置token')),
      ElevatedButton(
          onPressed: () async {
            final token = await tokenManager.getToken();
            PromptAction.showSuccess('token: $token');
          },
          child: const Text('获取token')),
      ElevatedButton(
          onPressed: () async {
            await tokenManager.removeToken();
            PromptAction.showSuccess('删除token成功');
          },
          child: const Text('删除token'))
    ],
  );
}
